
package com.example.chiro;

import java.text.Normalizer;
import java.util.Locale;

import android.R.drawable;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Spinner;

public class Slide_arbre extends Fragment {

    public static ImageButton photo_imageb;
    static RadioGroup arbre_hauteur, arbre_diametre, arbre_houppier, arbre_potentiel;
    static ImageView photo_check;
    static Spinner arbre_type, arbre_peuplement, arbre_statut, arbre_etat;
    
    public static ViewGroup root;
    static boolean photo_verouille = false;

    public static Fragment newInstance(Context context) {
        Log.i("t: Slide_arbre", "newInstance : debut");
        Slide_arbre f = new Slide_arbre();
        Log.i("t: Slide_arbre", "newInstance : fin");
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        Log.i("t: Slide_arbre", "oCView : debut");
        root = (ViewGroup) inflater.inflate(R.layout.slide_arbre, null);

        //Log.i("test", "test");
        photo_check = (ImageView) root.findViewById(R.id.arbre_imageview_photo);
		
        //Log.i("test2", "test2");
		
		
        arbre_type = (Spinner) root.findViewById(R.id.arbre_type);
        ArrayAdapter<CharSequence> adapter_type = ArrayAdapter.createFromResource(
                root.getContext(), R.array.arbre_type, android.R.layout.simple_spinner_item);
        adapter_type.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        arbre_type.setAdapter(adapter_type);

        arbre_peuplement = (Spinner) root.findViewById(R.id.arbre_peuplement);
        ArrayAdapter<CharSequence> adapter_peuplement = ArrayAdapter.createFromResource(
                root.getContext(), R.array.arbre_peuplement, android.R.layout.simple_spinner_item);
        adapter_peuplement.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        arbre_peuplement.setAdapter(adapter_peuplement);

        arbre_statut = (Spinner) root.findViewById(R.id.arbre_statut);
        ArrayAdapter<CharSequence> adapter_statut = ArrayAdapter.createFromResource(
                root.getContext(), R.array.arbre_statut, android.R.layout.simple_spinner_item);
        adapter_statut.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        arbre_statut.setAdapter(adapter_statut);

        arbre_etat = (Spinner) root.findViewById(R.id.arbre_etat);
        ArrayAdapter<CharSequence> adapter_etat = ArrayAdapter.createFromResource(
                root.getContext(), R.array.arbre_etat, android.R.layout.simple_spinner_item);
        adapter_etat.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        arbre_etat.setAdapter(adapter_etat);

        photo_imageb = (ImageButton) root.findViewById(R.id.arbre_imagebutton_photo);
        photo_imageb.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
            	if (photo_verouille){
    				Toast.makeText(Arbre.context, "Une photo a déjà été prise pour la fiche en cours!", Toast.LENGTH_SHORT).show();
    				return;
            	}
                Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                getActivity().startActivityForResult(intent, 1111);
            }
        });
       
        arbre_hauteur = (RadioGroup) root.findViewById(R.id.arbre_hauteur);        
        arbre_diametre = (RadioGroup) root.findViewById(R.id.arbre_diametre);       
        arbre_houppier = (RadioGroup) root.findViewById(R.id.arbre_houppier);
        arbre_potentiel = (RadioGroup) root.findViewById(R.id.arbre_potentiel);
        
        Log.i("t: Slide_arbre", "oCView : fin");
        return root;
    }
    
    public static ContentValues recolte_donnees(){
    	
    	//Spinner arbre_type, arbre_peuplement, arbre_statut, arbre_etat;	
    	
    	ContentValues initialValues = new ContentValues();
    	
    	initialValues.put(Table_arbre.PATH_PHOTO, String_operation.op_string(Arbre.photo_arbre_path));    
    	initialValues.put(Table_arbre.TYPE,String_operation.op_spin(arbre_type));
    	initialValues.put(Table_arbre.HAUTEUR,String_operation.op_radioG(arbre_hauteur,root));
    	initialValues.put(Table_arbre.DIAMETRE,String_operation.op_radioG(arbre_diametre,root));
    	initialValues.put(Table_arbre.STATUT,String_operation.op_spin(arbre_statut));
    	initialValues.put(Table_arbre.ETAT,String_operation.op_spin(arbre_etat));
    	initialValues.put(Table_arbre.PEUPLEMENT,String_operation.op_spin(arbre_peuplement));
    	initialValues.put(Table_arbre.HOUPPIER,String_operation.op_radioG(arbre_houppier,root));
    	initialValues.put(Table_arbre.POTENTIEL,String_operation.op_radioG(arbre_potentiel,root));
    	
    	
    	Arbre.photo_arbre_path = "";
    	deverouiller_photo();
    	
    	Log.i("RECOLTE_ARBRE", initialValues.toString());
    	
		return initialValues;
	}

	public static void verouiller_photo() {
		// TODO Auto-generated method stub
		photo_check.setImageResource(drawable.checkbox_on_background);
		
		AlphaAnimation alpha = new AlphaAnimation(0.5F, 0.5F);
		alpha.setDuration(0);
		alpha.setFillAfter(true);
		
		LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(photo_check.getWidth()*2, photo_check.getHeight()*2);
		photo_check.setLayoutParams(param);
		
		photo_imageb.startAnimation(alpha);
		photo_verouille = true;
	}

	public static void deverouiller_photo(){
		photo_check.setImageResource(drawable.stat_sys_warning);
		
		AlphaAnimation alpha = new AlphaAnimation(1F, 1F);
		alpha.setDuration(0);
		alpha.setFillAfter(true);
		
		LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(photo_check.getWidth()/2, photo_check.getHeight()/2);
		photo_check.setLayoutParams(param);
		
		photo_imageb.startAnimation(alpha);
		photo_verouille = false;
	}
	
	
	
}
